package cn.scu.ne04jdemo.controller;

import cn.scu.ne04jdemo.entitiy.ScheDevInfoEntity;
import cn.scu.ne04jdemo.entitiy.ScheDevTypeInfoEntity;
import cn.scu.ne04jdemo.entitiy.ScheOrderInfoEntity;
import cn.scu.ne04jdemo.service.ScheOrderInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sche/order/info")
public class ScheOrderInfoController {

    @Autowired
    private ScheOrderInfoService scheOrderInfoService;

    @GetMapping("/findPage")
    public ResponseEntity<IPage> findPage(@RequestParam(required = false) String id,
                                          @RequestParam(required = false) String orderName,
                                          @RequestParam(required = false) String orderCompany,
                                          @RequestParam(required = false) String orderType,
                                          @RequestParam(required = false) String orderState,
                                          @RequestParam(defaultValue = "1") int pageNum,
                                          @RequestParam(defaultValue = "10") int pageSize) {
        LambdaQueryWrapper<ScheOrderInfoEntity> queryWrapper = new QueryWrapper<ScheOrderInfoEntity>().lambda()
                .like(StringUtils.isNotBlank(id), ScheOrderInfoEntity::getId, id)
                .like(StringUtils.isNotBlank(orderName), ScheOrderInfoEntity::getOrderName, orderName)
                .eq(StringUtils.isNotBlank(orderType), ScheOrderInfoEntity::getOrderType, orderType)
                .eq(StringUtils.isNotBlank(orderState), ScheOrderInfoEntity::getOrderState, orderState)
                .like(StringUtils.isNotBlank(orderCompany), ScheOrderInfoEntity::getOrderCompany, orderCompany)
                .orderByDesc(ScheOrderInfoEntity::getCreateDate);

        IPage<ScheOrderInfoEntity> page = scheOrderInfoService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return ResponseEntity.ok(page);
    }

    @PostMapping("/insert")
    @ResponseBody
    public Boolean insert(@RequestBody ScheOrderInfoEntity scheOrderInfoEntity){
//        try {
//            if(!ObjectUtils.isNotNull(scheOrderInfoEntity))
//                scheOrderInfoEntity = new ScheOrderInfoEntity();
//            scheOrderInfoService.save(scheOrderInfoEntity);
//        }catch (Exception e){
//            e.printStackTrace();
//            return false;
//        }
//        return true;
        try {
            return this.scheOrderInfoService.insert(scheOrderInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

}
